package com.ruoyi.crm.controller.clue;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.crm.domain.Activity;
import com.ruoyi.crm.domain.Course;
import com.ruoyi.crm.domain.vo.CluePoolVO;
import com.ruoyi.crm.domain.vo.FalseClueVO;
import com.ruoyi.crm.domain.vo.ResourceGainVO;
import com.ruoyi.crm.service.IActivityService;
import com.ruoyi.crm.service.ICourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.service.IClueService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 线索管理Controller
 *
 * @author ruoyi
 * @date 2022-06-07
 */
@Slf4j
@RequestMapping("/clues")
@RestController("clueController")
public class ClueController extends BaseController
{
    @Autowired
    private IClueService clueService;
    @Autowired
    private IActivityService iActivityService;
    @Resource
    private ICourseService icourseService;

    /**
     * 查询线索管理列表
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:list')")
    @GetMapping("/clue/list")
    public TableDataInfo list(Clue clue)
    {
        startPage();
        List<Clue> list = clueService.selectClueList(clue);
        return getDataTable(list);
    }

    /**
     * 导出线索管理列表
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:export')")
    @Log(title = "线索管理", businessType = BusinessType.EXPORT)
    @PostMapping("/clue/export")
    public void export(HttpServletResponse response, Clue clue)
    {
        List<Clue> list = clueService.selectClueList(clue);
        ExcelUtil<Clue> util = new ExcelUtil<Clue>(Clue.class);
        util.exportExcel(response, list, "线索管理数据");
    }

    /**
     * 获取线索管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:query')")
    @GetMapping(value = "/clue/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(clueService.selectClueById(id));
    }

    /**
     * 新增线索管理
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:add')")
    @Log(title = "线索管理", businessType = BusinessType.INSERT)
    @PostMapping("/clue")
    public AjaxResult add(@RequestBody Clue clue)
    {
        return toAjax(clueService.insertClue(clue));
    }

    /**
     * 修改线索管理
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:edit')")
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Clue clue)
    {
        return toAjax(clueService.updateClue(clue));
    }

    /**
     * 删除线索管理
     */
    @PreAuthorize("@ss.hasPermi('crm:clue:remove')")
    @Log(title = "线索管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(clueService.deleteClueByIds(ids));
    }

    @GetMapping("/activity/listselect/{type}")
    public AjaxResult listselect(@PathVariable String type){
        return iActivityService.listselect(type);
    }

    /**
     * 转伪线索
     * @param clueId
     * @return
     */
    @PutMapping("/clue/false/{clueId}")
    public AjaxResult clueFalse(@PathVariable Long clueId,@RequestBody FalseClueVO falseClueVO){
        return clueService.clueFalse(clueId,falseClueVO);
    }

    /**
     * 查询当前登录人的  所有伪线索，如果是admin则查全部
     * 线索池
     */
    @GetMapping("/clue/pool")
    public TableDataInfo cluePool(Clue clue){
        startPage();
        List<CluePoolVO> list = clueService.selectFalseClueList(clue);
        return getDataTable(list);
    }

    @PutMapping("/clue/gain")
    public AjaxResult gain(@RequestBody ResourceGainVO clueGainVO){
        clueService.gain(clueGainVO);
        return AjaxResult.success("全部捞取成功");
    }

    /**
     * 线索转商机
     * @param clueId
     * @return
     */
    @PutMapping("/clue/changeBusiness/{clueId}")
    public AjaxResult changeBusiness(@PathVariable Long clueId){
        log.info("clueId：{}",clueId);
        clueService.changeBusiness(clueId);
        return AjaxResult.success();
    }

    /**
     * 线索分配
     * @param clueGainVO
     * @return
     */
    @PutMapping("/clue/assignment")
    public AjaxResult assignment(@RequestBody ResourceGainVO clueGainVO){
        return clueService.assignment(clueGainVO);
    }

    @GetMapping("/course/listselect")
    public AjaxResult courseListselectSubject(String subject){
        return clueService.courseListselect(subject);
    }

    /**
     * 活动列表
     */
    @GetMapping("/activity/list")
    public TableDataInfo activityList(Activity activity){
        startPage();
        List<Activity> list = iActivityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 课程管理
     */
    @GetMapping("/course/list")
    public TableDataInfo courseList(Course course){
        startPage();
        List<Course> courses = icourseService.selectCourseList(course);
        return getDataTable(courses);
    }
}
